﻿Public Class NhapChiTietDA
    Private cn As DatabaseConnect
    Private sql As String

    Public Sub New()
        cn = New DatabaseConnect()
    End Sub

    Public Function InNhapCT(ByVal nct As NhapChiTietDT)
        If nct.MaNCC <> "" Then
            sql = "INSERT INTO SanPham_Nhap_ChiTiet VALUES('" + nct.Ma + "'," & nct.STT & ", " & nct.MaLoai & ", " & nct.SoLuong & ", '" + nct.MaNCC + "', N'" + nct.GhiChu + "')"
        Else
            sql = "INSERT INTO SanPham_Nhap_ChiTiet(MaNhap,STT,MaLoai,soluong,ghichu) VALUES('" + nct.Ma + "'," & nct.STT & ", " & nct.MaLoai & ", " & nct.SoLuong & ", N'" + nct.GhiChu + "')"
        End If
        Return cn.Execute(sql)
    End Function

    Public Function UpNhapCT(ByVal nct As NhapChiTietDT)
        sql = "UPDATE SanPham_Nhap_ChiTiet SET MaNCC = '" + nct.MaNCC + "', ghichu = N'" + nct.GhiChu + "' WHERE MaNhap = '" + nct.Ma + "', STT = " & nct.STT
        Return cn.Execute(sql)
    End Function

    Public Function GetData(ByVal dieukien As String) As List(Of NhapChiTietDT)
        Dim nct As NhapChiTietDT
        Dim list As New List(Of NhapChiTietDT)
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM SanPham_Nhap_ChiTiet " + dieukien)

        For i As Integer = 0 To dt.Rows.Count() - 1
            Dim ma As String = dt.Rows(i)("MaNhap").ToString()
            Dim stt As Integer = Convert.ToInt32(dt.Rows(i)("STT"))
            Dim maloai As Integer = Convert.ToInt32(dt.Rows(i)("MaLoai"))
            Dim soluong As Double = Convert.ToDouble(dt.Rows(i)("soluong"))
            Dim ghichu As String = dt.Rows(i)("ghichu").ToString()
            Dim ncc As String = dt.Rows(i)("MaNCC").ToString()
            '-----'
            nct = New NhapChiTietDT(ma, maloai, stt, soluong, ncc, ghichu)
            list.Add(nct)
        Next
        Return list
    End Function

    Public Function TinhTongSoLuong(ByVal ma As String, ByVal loai As Integer) As Double
        Dim dt As New DataTable
        dt = cn.GetData("SELECT SUM(soluong) as Tong FROM SanPham_Nhap_ChiTiet WHERE MaNhap = '" + ma + "' AND MaLoai = " & loai)
        Dim tong As Double = Convert.ToDouble(dt.Rows(0)("Tong"))
        Return tong
    End Function

    Public Function GetSTT(ByVal ma As String) As Integer
        Dim stt As Integer = 1
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM SanPham_Nhap_ChiTiet WHERE MaNhap = '" + ma + "'")
        If dt.Rows.Count > 0 Then
            stt = dt.Rows.Count + 1
        End If
        Return stt
    End Function
End Class
